package newRegFrm.service;

import newRegFrm.dao.EmployeeDao;
import newRegFrm.dao.EmployeeDaoImpl;
import newRegFrm.model.Employee;
import newRegFrm.model.User;
import user.c3p0Util;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeService {
    public EmployeeDao employeeDao = new EmployeeDaoImpl();
    public User user = new User();
    // 注册新员工
    public boolean register(Employee employee) {
        Connection conn = null;
        try {
            conn = c3p0Util.getConnection();
            int result = employeeDao.addEmployee(conn, employee);
            // 明确判断返回值
            if (result == 1) {
                return true;
            } else if (result == 0) {
                // 用户名已存在
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            c3p0Util.closeConnection(conn);
        }
        return false;
    }

    // 检查用户名是否存在
    public boolean isNameExist(String ename) {
        Connection conn = null;
        try {
            conn = c3p0Util.getConnection();
            Employee employee = employeeDao.findByName(conn, ename);
            return employee != null;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            c3p0Util.closeConnection(conn);
        }
    }

    // 员工登录
    public Employee login(String ename, String password) {
        Connection conn = null;
        try {
            conn = c3p0Util.getConnection();
            return employeeDao.login(conn, ename, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            c3p0Util.closeConnection(conn);
        }
    }

    // 获取所有
    public List<Employee> getAll() {
        Connection conn = null;
        try {
            conn = c3p0Util.getConnection();
            return employeeDao.getAll(conn);
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据加载失败: "+e.getMessage());
            return null;
        } finally {
            c3p0Util.closeConnection(conn);
        }
    }

    // 通过名字获取
    public List<Employee> listEmployeeByUsername(String ename) throws SQLException {
        List<Employee> employees = new ArrayList<>();
        String sql = "SELECE empno,job,mgr,hiredata,sal,ofee,deptno,role,password FROM employee WHERE ename = ?";
        try (Connection conn = c3p0Util.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, ename);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setEmpno(rs.getInt("empno"));
                    employee.setJob(rs.getString("job"));
                    employee.setMgr(rs.getString("mgr"));
                    employee.setHiredata(rs.getDate("hiredata"));
                    employee.setSal(rs.getInt("sal"));
                    employee.setOfee(rs.getInt("ofee"));
                    employee.setDeptno(rs.getInt("deptno"));
                    employee.setRole(rs.getInt("role"));
                    employee.setPassword(rs.getString("password"));

                }
            }
            return employees;
        }
    }
}
